package yxy.apple.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;

public class CaseInsensitiveHashMap<V> extends HashMap<String, V> {

	private HashMap<String, String> originalKeys = new HashMap<>();
	private static final long serialVersionUID = -5333788427308044121L;

	@Override
	public V get(Object key) {
		String original = key.toString();
		String lowercase = original.toLowerCase();
		return super.get(lowercase);
	}

	@Override
	public V remove(Object key) {
		String original = key.toString();
		String lowercase = original.toLowerCase();
		this.originalKeys.remove(original);
		return super.remove(lowercase);
	}

	@Override
	public boolean containsKey(Object key) {
		return this.originalKeys.containsKey(key.toString());
	}

	@Override
	public Set<String> keySet() {
		return this.originalKeys.keySet();
	}

	@Override
	public V put(String key, V value) {
		String original = key.toString();
		String lowercase = original.toLowerCase();
		this.originalKeys.put(original, original);
		return super.put(lowercase, value);
	}

	@Override
	public void putAll(Map<? extends String, ? extends V> map) {
		Iterator<? extends String> keys = map.keySet().iterator();
		while (keys.hasNext()) {
			String original = keys.next();
			this.put(original, map.get(original));
		}
	}

	@Override
	public boolean replace(String key, V oldValue, V newValue) {
		return super.replace(key.toLowerCase(), oldValue, newValue);
	}

	@Override
	public V replace(String key, V value) {
		return super.replace(key.toLowerCase(), value);
	}

	@Override
	@Deprecated
	/**
	 * @Deprecated 暂未完全实现
	 */
	public V compute(String key, BiFunction<? super String, ? super V, ? extends V> remappingFunction) {
		return super.compute(key, remappingFunction);
	}

	@Override
	@Deprecated
	/**
	 * @Deprecated 暂未完全实现
	 */
	public V computeIfAbsent(String key, Function<? super String, ? extends V> mappingFunction) {
		return super.computeIfAbsent(key, mappingFunction);
	}

	public V computeIfPresent(String key, BiFunction<? super String, ? super V, ? extends V> remappingFunction) {
		return this.computeIfPresent(key, remappingFunction);
	}

	@Override
	@Deprecated
	/**
	 * @Deprecated 暂未完全实现
	 */
	public V merge(String key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
		return super.merge(key, value, remappingFunction);
	}

	@Override
	@Deprecated
	/**
	 * @Deprecated 暂未完全实现
	 */
	public void forEach(BiConsumer<? super String, ? super V> action) {
		super.forEach(action);
	}

	@Override
	@Deprecated
	/**
	 * @Deprecated 暂未完全实现
	 */
	public void replaceAll(BiFunction<? super String, ? super V, ? extends V> function) {
		super.replaceAll(function);
	}

	@Override
	public Object clone() {
		@SuppressWarnings("unchecked")
		CaseInsensitiveHashMap<V> result = (CaseInsensitiveHashMap<V>) super.clone();
		result.originalKeys.putAll(this.originalKeys);
		return result;
	}
}
